
<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" label-width="auto" :inline="true">
        <el-form-item label="城市" prop="cityId">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.cityId" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in cityOptions" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon><Search /></template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon><Refresh /></template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table v-loading="loading" :data="TableData" highlight-current-row row-key="id" @row-click="handleRowClick">
        <el-table-column label="城市" min-width="120" align="center" prop="cityName"></el-table-column>
        <el-table-column label="排名配置" min-width="80" align="center" prop="gradeDTO">
          <template #default="scope">
            <div v-for="(item, i) in scope.row.gradeDTO" :key="i">
              <div>级别:{{ item.grade }}-完单量配置{{ item.num }}-延迟时间{{ item.time }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" align="center" label="操作" width="220">
          <template #default="scope">
            <div>
              <el-button type="primary" link size="small" @click.stop="handleConfig(scope.row)">
                <template #icon><Pointer /></template>
                配置
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
    </el-card>
    <!-- 改派 -->
    <EditModal ref="editModalRef" @success="handleQuery" />
  </div>
</template>

<script setup lang="ts">
import CommonApi from "@/api/common";
import EditModal from "./components/editModal.vue";
import { carTypeArr, xcCarTypeArr, fzCarTypeArr, nhCarTypeArr, hnCarTypeArr, xcCarEnum, fzCarTypeEnum, nhCarTypeEnum, hvCarEnum } from "@/enums/common";
import { MenuTypeEnum } from "@/enums/MenuTypeEnum";
import DriverApi from "@/api/driver";

const queryFormRef = ref(ElForm);
const total = ref(0);
const supplierOptions = ref();
const cityOptions = ref();
const useTimes = ref();
const createdTimes = ref();
const editModalRef = ref(null);

const loading = ref(false);

// 查询参数
const queryParams = reactive<MenuQuery>({
  pageNo: 1,
  pageSize: 10,
});
// 菜单表格数据
const TableData = ref([]);

// 选择表格的行菜单ID
const selectedMenuId = ref<number | undefined>();

// 查询菜单
function handleQuery() {
  loading.value = true;
  DriverApi.getDriverRank(queryParams)
    .then((data) => {
      TableData.value = data.result;
      total.value = data.total * 1;
    })
    .finally(() => {
      loading.value = false;
    });
}
//点击配置触发
function handleConfig(row: any) {
  editModalRef.value.edit(row);
}

function handleUseTimeChange(time: any) {
  if (time) {
    queryParams.useTimeFrom = `${time[0]} 00:00:00`;
    queryParams.useTimeTo = `${time[1]} 23:59:59`;
  } else {
    queryParams.useTimeFrom = undefined;
    queryParams.useTimeTo = undefined;
  }
}
function handleCreatedTimeChange(time: any) {
  if (time) {
    queryParams.createTimeFrom = `${time[0]} 00:00:00`;
    queryParams.createTimeTo = `${time[1]} 23:59:59`;
  } else {
    queryParams.createTimeFrom = undefined;
    queryParams.createTimeFrom = undefined;
  }
}

// 重置查询
function handleResetQuery() {
  queryParams.useTimeFrom = undefined;
  queryParams.useTimeTo = undefined;
  queryParams.createTimeFrom = undefined;
  queryParams.createTimeTo = undefined;
  useTimes.value = null;
  createdTimes.value = null;

  queryFormRef.value.resetFields();
  queryParams.pageNo = 1;
  handleQuery();
}

// 行点击事件
function handleRowClick(row: MenuVO) {
  // 记录表格选择的菜单ID，新增子菜单作为父菜单ID
  selectedMenuId.value = row.id;
}

onMounted(() => {
  handleQuery();
  CommonApi.getSupplier({ pageNo: 1, pageSize: 9999, status: 1 }).then((data) => {
    if (data.code == 0) {
      supplierOptions.value = data.result;
    }
  });
  CommonApi.getCity({ pageNo: 1, pageSize: 9999 }).then((data) => {
    if (data.code == 0) {
      cityOptions.value = data.result;
    }
  });
});
</script>
<style lang="scss" scoped>
.red_rate {
  color: red;
}
.address_box {
  display: flex;
  flex-direction: column;
  align-items: left;
  justify-content: left;
  .item {
    display: flex;
    align-items: center;
    margin: 4px 0;
    .tag {
      width: 50px;
      height: 23px;
      text-align: center;
      color: orange;
      background: #fff8e6;
    }
    .tag2 {
      color: #3fd175;
      background: #e7faf0;
    }
  }
}
</style>
